<?php
/**
 * 注册服务层
 * 2019.3.11
 * xiaosu
 */
namespace app\api\service;

use think\Db;

class RegisterService
{
	/**
	 * 验证手机
	 * $type  1-注册，2-修改密码
	 */
	public function checkMobile($mobile,$type)
	{
		$is_has = Db::name('shop_user')->where('user_name', $mobile)->find();
		if ($type == 1) {
			if ($is_has) {
				return ['status' => false, 'msg' => '该手机已经注册'];
			}
		} else {
			if (empty($is_has)) {
				return ['status' => false, 'msg' => '该手机未注册'];
			}

		}

		$last_code = Db::name('sms_validate')->where('mobile', $mobile)->order('id DESC')->find();
		//一分钟只能获取一次
		if ($last_code) {
			if ($last_code['create_time']+60 > time()) {
				return ['status' => false, 'msg' => '一分钟只能获取一次验证码'];
			}
		}
		//一天只能五条
		$count = Db::name('sms_validate')->where('mobile', $mobile)->where('create_time','between',[strtotime(date('Y-m-d',time())), time()])->count();
		
		if ($count > 4) {
			return ['status' => false, 'msg' => '每天获取验证码上线'];
		}

		return ['status' => true];
	}
	/**
	 * 添加验证码
	 */
	public function addCode($data)
	{
		$res = Db::name('sms_validate')->insert($data);
		return $res;
	}
	/**
	 * 获取邀请人信息
	 */
	public function getInvitation($mobile)
	{
		$info = Db::name('shop_user')->where('user_name', $mobile)->find();
		if (empty($info)) {
			return  false;
		}
		return $info;
	}
	/**
	 * 添加用户
	 */
	public function addUser($data)
	{
		$res = Db::name('shop_user')->insert($data);
		return $res;
	}
	/**
     * 获取用户
     */
    public function getUserInfo($mobile)
    {
    	$info = Db::name('shop_user')->where('user_name', $mobile)->find();
    	return $info;
    }
    /**
     * 检查验证码
     */
    public function checkCode($mobile,$code,$type)
    {
    	$info = Db::name('sms_validate')->where(['mobile' => $mobile, 'code' => $code, 'scene' => $type])->find();
    	if (empty($info)) {
    		return ['status' => false, 'msg' => '验证码错误'];
    	}
  		if ($info['expire_time'] <time()) {
  			return ['status' => false, 'msg' => '验证码过期'];
  		}
  		return ['status' => true, 'msg' => '操作成功', 'key' => md5($mobile.$type.time())];
    }
    /**
     * 修改密码
     */
    public function updateUser($where,$data)
    {
    	$res = Db::name('shop_user')->where($where)->update($data);
    	return $res;
    }

	/**
	 * 获取地址
	 */
	public function getAddress()
	{	
		$list = Db::name('region')->alias('a')->field('a.*,b.parent_id as grandpa_id')->join('region b', 'a.parent_id=b.id', 'left')->select();

		$address_list = [];
		foreach ($list as $li) {
            $li['addr_id'] = $li['id'];
            unset($li['id']);
			switch ($li['level']) {
				case '1':
					$address_list[$li['addr_id']] = $li;
					break;
				case '2':
					$address_list[$li['parent_id']]['child'][$li['addr_id']] = $li;
					break;
				case '3':
					$address_list[$li['grandpa_id']]['child'][$li['parent_id']]['child'][] = $li;
				default:
					break;
			}
		}
		sort($address_list,1);
        foreach ($address_list as $k=>$item) {
            $item['grandpa_id'] = $item['grandpa_id'] ? $item['grandpa_id'] : '';
            if(isset($item['child'])){
                $child = array_values($item['child']);
                sort($child,1);
                $item['child'] = $child;
            }else{
                $item['child'] = array();
            }

            $address_list[$k] = $item;
		}
		return $address_list;
	}

    /**
     * 提供给H5的省市区数据，返回的字段名需固定，以适应前端的三级联动脚本
     * 固定为：value   text   children
     * @author xuxh
     * @return array
     */
    public function getH5Address()
    {
        $list = Db::name('region')->alias('a')->field('a.*,b.parent_id as grandpa_id')->join('region b', 'a.parent_id=b.id', 'left')->select();

        $address_list = [];
        foreach ($list as $li) {
            $li['value'] = $li['id'];
            $li['text'] = $li['name'];
            unset($li['id'],$li['name']);
            switch ($li['level']) {
                case '1':
                    $address_list[$li['value']] = $li;
                    break;
                case '2':
                    $address_list[$li['parent_id']]['children'][$li['value']] = $li;
                    break;
                case '3':
                    $address_list[$li['grandpa_id']]['children'][$li['parent_id']]['children'][] = $li;
                default:
                    break;
            }
        }
        sort($address_list,1);
        foreach ($address_list as $k=>$item) {
            $item['grandpa_id'] = $item['grandpa_id'] ? $item['grandpa_id'] : '';
            if(isset($item['children'])){
                $child = array_values($item['children']);
                sort($child,1);
                $item['children'] = $child;
            }else{
                $item['children'] = array();
            }

            $address_list[$k] = $item;
        }
        return $address_list;
    }

}